package com.jinzhi.jzweb.controller;


import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.*;

import com.jinzhi.common.domain.ConfigDO;
import com.jinzhi.jzweb.domain.JazGroupSpecsDO;
import com.jinzhi.jzweb.domain.vo.AuntUserVO;
import com.jinzhi.jzweb.domain.vo.JazGroupProdAddVo;
import com.jinzhi.jzweb.domain.vo.JazGroupProdVo;
import com.jinzhi.jzweb.service.JazCoursesChapterService;
import com.jinzhi.jzweb.service.JazGroupSpecsService;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazGroupProdDO;
import com.jinzhi.jzweb.service.JazGroupProdService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 团购商品表
 * </pre>
 * <small> 2024-05-11 13:54:18 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazGroupProd")
public class JazGroupProdController extends BaseController {
	@Autowired
	private JazGroupProdService jazGroupProdService;

	@Autowired
	private JazGroupSpecsService jazGroupSpecsService;

	@ApiOperation(value = "商品列表页面入口", notes = "商品列表页面入口")
	@ApiImplicitParams({
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@GetMapping("")
	@RequiresPermissions("jzweb:jazGroupProd:selGroupProdList")
	public String JazGroupProd(){
		return "jzweb/jazGroupProd/jazGroupProd";
	}
	
	@ResponseBody
//	@GetMapping("/list")
//	@RequiresPermissions("jzweb:jazGroupProd:jazGroupProd")
	public Result<Page<JazGroupProdDO>> list(JazGroupProdDO jazGroupProdDTO){
		// 查询列表数据
		Page<JazGroupProdDO> page = jazGroupProdService.selectPage(getPage(JazGroupProdDO.class),
					jazGroupProdService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				JazGroupProdDO obj = (JazGroupProdDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	/**
	 *  商品列表查询
	 * @return
	 */
	@GetMapping("/selGroupProdList")
	@ResponseBody
	@RequiresPermissions("jzweb:jazGroupProd:selGroupProdList")
	public Result<Page<JazGroupProdDO>> selGroupProdList(@RequestParam(value = "name",required = false) String name,
									  @RequestParam("current")int current,
									  @RequestParam("size")int size)  {
		return jazGroupProdService.selGroupProdList(name, current, size);
	}

	/**
	 *  商品详情
	 * @return
	 */
	@GetMapping("/getInfo")
	@RequiresPermissions("jzweb:jazGroupProd:getInfo")
	public Result<JazGroupProdVo> getInfo(@RequestParam("id")Long id )  {
		JazGroupProdVo vo=jazGroupProdService.getInfo(id);
		return Result.ok(vo);

	}
	/**
	 * 上下架
	 * @return
	 */
	@Log(value = "团购商品上下架",sysModule = 6,operationType = 3)
	@GetMapping("/editStatus")
	@ResponseBody
//	@RequiresPermissions("jzweb:jazGroupProd:editStatus")
	public Result<String> editStatus(@RequestParam("id")Long id){
		JazGroupProdDO jazGroupProdDO = jazGroupProdService.selectById(id);
		if (jazGroupProdDO.getProdStatus().equals(1)){
			jazGroupProdDO.setProdStatus(0);
		}else {
			jazGroupProdDO.setProdStatus(1);
		}
		jazGroupProdService.updateById(jazGroupProdDO);
		return Result.ok();
	}

	/**
	 * 审核
	 * @return
	 */
	@Log(value = "团购商品审核",sysModule = 6,operationType = 3)
	@GetMapping("/checkGroup")
	@ResponseBody
	public Result<String> checkGroup(@RequestParam("id")Long id,@RequestParam("state")Integer state,
									 @RequestParam("result")String result){
		JazGroupProdDO jazGroupProdDO = jazGroupProdService.selectById(id);
		jazGroupProdDO.setCheckStatus(state);
		jazGroupProdDO.setCheckResult(result);
		jazGroupProdDO.setProdStatus(1);
		jazGroupProdService.updateById(jazGroupProdDO);
		return Result.ok();
	}

	@Log(value = "添加团购商品",sysModule = 6,operationType = 1)
	@ResponseBody
	@PostMapping("/saveGroupProd")
//	@RequiresPermissions("jzweb:jazGroupProd:saveGroupProd")
	public Result<String> saveGroupProd(@RequestBody JazGroupProdVo jazGroupProd) throws ParseException {
		if (jazGroupProd.getPeopleNum()<=1){
			return Result.fail("拼团人数最少2两个人");
		}
		jazGroupProdService.saveGroupProd(jazGroupProd);
		return Result.ok();
	}

	@Log(value = "修改团购商品",sysModule = 6,operationType = 3)
	@ResponseBody
	@PostMapping("/updateGroupProd")
//	@RequiresPermissions("jzweb:jazGroupProd:updateGroupProd")
	public Result<String> updateGroupProd(@RequestBody JazGroupProdVo jazGroupProd)throws ParseException{
		if (jazGroupProd.getPeopleNum()<=1){
			return Result.fail("拼团人数最少2两个人");
		}
		jazGroupProdService.updateGroupProd(jazGroupProd);
		return Result.ok();
	}

	@GetMapping("/add/{type}/{id}")
	String add(@PathVariable("type") String type,@PathVariable("id") Long id, Model model) {
		model.addAttribute("type", type);
		JazGroupProdDO jazGroupProdDO=jazGroupProdService.selectById(id);
		model.addAttribute("myData", jazGroupProdDO);
		model.addAttribute("userId", id);
		List<JazGroupSpecsDO> lists=jazGroupSpecsService.selectList(new EntityWrapper<JazGroupSpecsDO>().eq("group_prod_id", id));
		if(null==lists||lists.size()==0){
			lists.add(new JazGroupSpecsDO());
		}
		String images="";
		if(null!=jazGroupProdDO && null!=jazGroupProdDO.getId()){
			images=jazGroupProdDO.getImage();
		}
		String pictures="";
		if(null!=jazGroupProdDO && null!=jazGroupProdDO.getId()){
			pictures=jazGroupProdDO.getPicture();
		}
		model.addAttribute("images",images.split(","));
		model.addAttribute("pictures",pictures.split(","));
		model.addAttribute("voList",lists);
	    return "jzweb/jazGroupProd/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazGroupProd:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazGroupProdDO jazGroupProd = jazGroupProdService.selectById(id);
		model.addAttribute("jazGroupProd", jazGroupProd);
	    return "jzweb/jazGroupProd/edit";
	}
	

	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazGroupProd:add")
	public Result<String> save( JazGroupProdDO jazGroupProd){
		jazGroupProdService.insert(jazGroupProd);
        return Result.ok();
	}
	
//	@Log("修改团购商品表")
//	@ResponseBody
//	@RequestMapping("/update")
//	@RequiresPermissions("jzweb:jazGroupProd:edit")
//	public Result<String>  update( JazGroupProdDO jazGroupProd){
//		boolean update = jazGroupProdService.updateById(jazGroupProd);
//		return update ? Result.ok() : Result.fail();
//	}
	
	@Log(value = "删除团购商品",sysModule = 6,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Long id){
		jazGroupProdService.deleteById(id);
        return Result.ok();
	}
	
	@Log(value = "批量删除团购商品",sysModule = 6,operationType = 2)
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazGroupProd:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		jazGroupProdService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
